package follow.juc.heima.day7.Implementthread;

import jdk.nashorn.internal.ir.CallNode;

import java.util.concurrent.locks.ReentrantLock;

public class Demo5Ticket {
    public static void main(String[] args) {
        //创建线程任务对象
        Ticket ticket = new Ticket();
        Thread t1 = new Thread(ticket);
        Thread t2 = new Thread(ticket);
        Thread t3 = new Thread(ticket);

        t1.start();
        t2.start();
        t3.start();

    }

    static class Ticket implements Runnable {

        ReentrantLock lock = new ReentrantLock();
        private int ticket = 10;

        @Override
        public void run() {
            String name = Thread.currentThread().getName();
            while (true) {
                sell(name);
                if (ticket < 0) {
                    break;
                }
            }
        }

        Object lock1 = new Object();

        private synchronized void sell(String name) {
            synchronized (this) {
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (ticket > 0) {
                    System.out.println(name + "卖票:" + ticket);
                    ticket--;
                }

            }


        }
    }
}
